package scsys.webServlet.main;


import com.alibaba.fastjson.JSON;
import scsys.pojo.Course;
import scsys.pojo.Elective;
import scsys.pojo.User;
import scsys.pojo.chooseCourse;
import scsys.servlet.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/chooseCourseServlet")
public class chooseCourseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 数据
        UserService userService = new UserServiceImpl();
        CourseService courseService = new CourseServiceImpl();
        EletiveService eletiveService = new EletiveServiceImpl();

        // 获取当前用户
        // 1. 接收数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        // 2.转为User对象
        User user = JSON.parseObject(params, User.class);
        // 3.数据库获取当用户
        user = userService.selectOne(user);

        // 提取所有课程信息
        List<Course> courses = new ArrayList<Course>();
        courses = courseService.selectAll();

        // 提取自己已选课程信息
        Elective elective = new Elective();
         elective.setUserName(user.getUserName());
        List<Elective> usereles = new ArrayList<Elective>();
        usereles = eletiveService.selectMore(elective);


        // 2.以全部课程为索引，提取选课信息
        List<chooseCourse> chooseCourses = new ArrayList<chooseCourse>();
        for (Course cours : courses) {
            chooseCourse cc = new chooseCourse();
            // 3.添加信息
            cc.setCourse_id(cours.getId());
            cc.setCourse_name(cours.getName());
            cc.setTeacher(cours.getTeacher());
            cc.setPoint(cours.getPoint() + "");
            cc.setLocation(cours.getLocation());
            cc.setLimited(cours.getLimited() + "");
            cc.setClass_time("");
            // 获取当前课程中选课d人数
            Elective elective1 = new Elective();
            elective1.setCourseId(cours.getId());
            List<Elective> electives = eletiveService.selectMore(elective1);
            cc.setSelect_number(electives.size() + "");

            // 4.添加上课时间
            switch (cours.getTime1().charAt(0)){
                case '1': {
                    cc.setClass_time(cc.getClass_time() + "星期一 ");
                    break;
                }
                case '2': {
                    cc.setClass_time(cc.getClass_time() + "星期二 ");
                    break;
                }
                case '3': {
                    cc.setClass_time(cc.getClass_time() + "星期三 ");
                    break;
                }
                case '4': {
                    cc.setClass_time(cc.getClass_time() + "星期四 ");
                    break;
                }
                case '5': {
                    cc.setClass_time(cc.getClass_time() + "星期五 ");
                    break;
                }
            }
            switch (cours.getTime1().charAt(1)){
                case '1': {
                    cc.setClass_time(cc.getClass_time() + "8:05~9:50\n");
                    break;
                }
                case '2': {
                    cc.setClass_time(cc.getClass_time() + "10:15~12:00\n");
                    break;
                }
                case '3': {
                    cc.setClass_time(cc.getClass_time() + "13:35~15:20\n");
                    break;
                }
                case '4': {
                    cc.setClass_time(cc.getClass_time() + "15:45~17:30\n");
                    break;
                }
                case '5': {
                    cc.setClass_time(cc.getClass_time() + "18:30~20:45\n");
                    break;
                }
            }
            // 时间二-------------------------------------
            switch (cours.getTime2().charAt(0)){
                case '1': {
                    cc.setClass_time(cc.getClass_time() + "星期一 ");
                    break;
                }
                case '2': {
                    cc.setClass_time(cc.getClass_time() + "星期二 ");
                    break;
                }
                case '3': {
                    cc.setClass_time(cc.getClass_time() + "星期三 ");
                    break;
                }
                case '4': {
                    cc.setClass_time(cc.getClass_time() + "星期四 ");
                    break;
                }
                case '5': {
                    cc.setClass_time(cc.getClass_time() + "星期五 ");
                    break;
                }
            }
            switch (cours.getTime2().charAt(1)){
                case '1': {
                    cc.setClass_time(cc.getClass_time() + "8:05~9:50");
                    break;
                }
                case '2': {
                    cc.setClass_time(cc.getClass_time() + "10:15~12:00");
                    break;
                }
                case '3': {
                    cc.setClass_time(cc.getClass_time() + "13:35~15:20");
                    break;
                }
                case '4': {
                    cc.setClass_time(cc.getClass_time() + "15:45~17:30");
                    break;
                }
                case '5': {
                    cc.setClass_time(cc.getClass_time() + "18:30~20:45");
                    break;
                }
            }

            // 5.判断是否为可选课程
            if(usereles.size() != 0) {
                for (int i = 0; i < usereles.size(); i++) {
                    if (cours.getId().equals(usereles.get(i).getCourseId())) {
                        // 6.本用户已选该课程 按钮为退课
                        cc.setOperate("退课");
                        break;
                    } else {
                        if (cc.getSelect_number().equals(cours.getLimited() + "")) {
                            // 已满
                            cc.setOperate("不选课");
                        } else {
                            cc.setOperate("选课");
                        }
                    }
                }
            }else {
                if (cc.getSelect_number().equals(cours.getLimited() + "")) {
                    // 已满
                    cc.setOperate("不选课");
                }else {
                    cc.setOperate("选课");
                }
            }
            chooseCourses.add(cc);
        }


        // 8.发送到选课网页
        //2. 转为JSON
//        System.out.println("cccc" + chooseCourses);
        String jsonString = JSON.toJSONString(chooseCourses);
        chooseCourses = null;
        user = null;
        elective = null;
        usereles = null;
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
